//
//  EFGoodScaleViewCell.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/25.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFGoodScaleViewCell.h"
#import "EFGoodScaleViewItem.h"
#import "EFGoodsModelItem.h"

@interface EFGoodScaleViewCell()

@property(strong,nonatomic)UIView* lineView;

@property(strong,nonatomic)NSMutableArray* btnArray;


@end

@implementation EFGoodScaleViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

-(void)setObject:(EFGoodScaleViewItem *)item{
    int left =   20;
    int top = 14;
    for (EFGoodsModelItem *goodsModelItem in  item.model) {
        UIButton* btn = [self getModelBtn:goodsModelItem];
        btn.left = left;
        btn.top = top;
        left += btn.width + 6;
        
        [self.contentView addSubview:btn];
        [self.btnArray addObject:btn];
        
        
        self.lineView.left = 20;
        self.lineView.top = btn.bottom + 15;
    }
   
    
    
}

-(void)initView{
    [self.contentView addSubview:self.lineView];
    self.btnArray = [NSMutableArray array];
}

-(void)onClickSelectBtn:(UIButton*)sender{
    sender.selected = ! sender.selected ;
    if(sender.selected){
        [sender setBackgroundColor:RGBCOLOR(0x69BA45)];
        
        for(UIButton* btn in self.btnArray){
            if(btn != sender){
                [btn setSelected:false];
                [btn setBackgroundColor:RGBCOLOR(0xF3F3F3)];
            }
        }
        
    }else{
        [sender setBackgroundColor:RGBCOLOR(0xF3F3F3)];
    }
    
    if(self.selectblock != NULL){
        self.selectblock(sender.titleLabel.text);
    }
     
}

-(UIButton*)getModelBtn:(EFGoodsModelItem*)goodItem{
    UIButton* btn = [[UIButton alloc]init];
    [btn setTitleColor:kBlack999 forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [btn setBackgroundColor:RGBCOLOR(0xF3F3F3)]; 
    btn.titleLabel.textColor = kBlack999;
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.layer.cornerRadius = 5;
    [btn setTitle:goodItem.model_value forState:UIControlStateNormal];
    [btn setSelected:goodItem.select];
    
    [btn sizeToFit];
    btn.width += 10;
    
    if(goodItem.select){
        [btn setBackgroundColor:RGBCOLOR(0x69BA45)];
    }else{
        [btn setBackgroundColor:RGBCOLOR(0xF3F3F3)];
    }
    
    [btn addTarget:self action:@selector(onClickSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}


-(UIView*)lineView{
    if(_lineView == NULL){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _lineView.backgroundColor = kBlackDDD;
        
    }
    return _lineView;
}



@end
